package cn.nawang.ebeim.client.version;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.nawang.ebeim.client.bean.DbVersionTreeBean;

public class VersionTree implements Serializable {

  private static final long serialVersionUID = 3L;

  private DirectoryNode root = new DirectoryNode("/");
  private transient List<DbVersionTreeBean> beans = new ArrayList<DbVersionTreeBean>();

  public static int count = 0;

  public DirectoryNode getRoot() {
    return root;
  }

  public void setRoot(DirectoryNode root) {
    this.root = root;
  }

  public void visit() {
    root.visit();
  }

  public void merge(VersionTree vt) {
    root.merge(vt.getRoot());
  }
  
  public void addToList(String dsId,String version){
    root.addToList(dsId,version,beans);
  }
  
  public void mergeAll(VersionTree vt){
    root.mergeAll(vt.getRoot());
  }

  public List<Node> takeChangeList() {
    List<Node> list = new ArrayList<Node>();
    root.filterChanges(list);

    return list;
  }

  public List<Node> takeNodeList() {
    List<Node> list = new ArrayList<Node>();
    root.filter(list, new EmptyFilter());
    return list;
  }

  public Node findNode(String path) {
    System.out.println("path-------"+path);
    String[] strs = path.split("\\\\");
    Map<String, Node> map = root.getChildren();
    for (int i = 0; i < strs.length; i++) {
      System.out.println(strs[i]);
      Node n = map.get(strs[i]);
      System.out.println(n);
      System.out.println("----");
      if (n != null) {
        if (i == strs.length - 1) {
          return n;
        } else {
          if (n instanceof DirectoryNode) {
            map = ((DirectoryNode) n).getChildren();
          } else {
            return null;
          }
        }
      } else {
        continue;
      }

    }
    return null;
  }

  @Override
  public String toString() {
    return "VersionTree [root=" + root + "]";
  }

  public List<DbVersionTreeBean> getBeans() {
    return beans;
  }
  
  
}
